let wxparse = require("../../wxParse/wxParse.js");


Page({
  data: {
    artId: '',
    artName: '',
    publishDate: '',
    source: '',
    like: '',
    notLike: '',
    zanImage:'',
    caiImage:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    let data = decodeURIComponent(options.news)
    let artId = options.artId
    if (artId) {
      wx.request({
        url: getApp().globalData.myHttp + '/news/getInfo/' + artId,
        method: "GET",
        header: {
          'content-type': 'application/json',
        },
        success(res) {
          if (res.data.success) {
            var contents = res.data.obj.info.contents
            wxparse.wxParse('contents', 'html', contents, that, 5)
          }
          that.setData({
            artId: res.data.obj.info.artID,
            artName: res.data.obj.info.artName,
            publishDate: res.data.obj.info.publishDate,
            source: res.data.obj.info.source,
            like: res.data.obj.info.myLike,
            notLike: res.data.obj.info.myUnLike
          })
        }
      });
    }

    if (data != null && data != "" && data != 'undefined') {
      var news = JSON.parse(data)
      var contents = news.contents
      
      wxparse.wxParse('contents', 'html', contents, this, 5)
      this.setData({
        artId: news.artID,
        artName: news.artName,
        publishDate: news.publishDate,
        source: news.source,
        like: news.myLike,
        notLike: news.myUnLike
      })
    }

    if(that.data.like == 1){
      that.setData({
        zanImage:'/image/zan@select.png'
      })
    }else{
      that.setData({
        zanImage:'/image/zan.png'
      })
    }
    if(that.data.notLike == 1){
      that.setData({
        caiImage:'/image/cai@select.png'
      })
    }else{
      that.setData({
        caiImage:'/image/cai.png'
      })
    }

  },
  //点赞
  like: function () {
    var that = this
    var user = wx.getStorageSync("user")
    var userId = user.userID
    var artId = that.data.artId
    if (that.data.notLike) {
      wx.showToast({
        title: '您已踩过',
        icon:'none',
        duration: 2000
      })
      return;
    }
    if (user !== '' && user !== null & user !== undefined) {
      if (that.data.like == 1) {
        this.removeLikeOrUnLike(artId, 1, userId)
      } else {
        this.addLikeOrUnLike(artId, 1, userId)
      }
    } else {
      wx.showToast({
        title: '请先登录!',
        icon:'none',
        duration: 2000
      })
    }

  },
  //踩
  notLike: function () {
    var that = this
    var user = wx.getStorageSync("user")
    var userId = user.userID
    var artId = that.data.artId

    if (that.data.like) {
      wx.showToast({
        title: '您已点过赞',
        icon:'none',
        duration: 2000
      })
      return;
    }
    if (user !== '' && user !== null & user !== undefined) {
      if (that.data.notLike == 1) {
        this.removeLikeOrUnLike(artId, 0, userId)
      } else {
        this.addLikeOrUnLike(artId, 0, userId)
      }
    } else {
      wx.showToast({
        title: '请先登录!',
        icon:'none',
        duration: 2000
      })
    }

  },
  //点赞、踩
  addLikeOrUnLike(artId, isLike, userId) {
    var that = this
    wx.request({
      url: getApp().globalData.myHttp + '/news/addLikeOrUnLike?artID='+artId+"&isLike="+isLike+"&userID="+userId,
      method: "POST",
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.data.success) {
          if(isLike == 1){
            that.setData({
              like: 1,
              zanImage:'/image/zan@select.png'
            })
          }else{
            that.setData({
              notLike: 1,
              caiImage:'/image/cai@select.png'
            })
          }
        }
      }
    });
  },
  //取消点赞、踩
  removeLikeOrUnLike(artId, isLike, userId) {
    var that = this
    wx.request({
      url: getApp().globalData.myHttp + '/news/removeLikeOrUnLike?artID='+artId+"&isLike="+isLike+"&userID="+userId,
      method: "POST",
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.data.success) {
          if(isLike == 1){
            that.setData({
              like: 0,
              zanImage:'/image/zan.png'
            })
          }else{
            that.setData({
              notLike: 0,
              caiImage:'/image/cai.png'
            })
          }
        }
      }
    });
  },
})